package com.gaussian.property.module.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaussian.property.module.system.entity.SysNotice;
import com.gaussian.property.module.system.mapper.SysNoticeMapper;
import com.gaussian.property.module.system.service.ISysNoticeService;
import com.gaussian.property.module.system.vo.SysNoticeAddVo;
import com.gaussian.property.module.system.vo.SysNoticeDetailVo;
import com.gaussian.property.module.system.vo.SysNoticeEditVo;
import com.gaussian.property.module.system.vo.SysNoticeQueryVo;
import com.songqi.common.core.domain.page.PageDomain;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.core.utils.BeanCopyUtil;
import com.songqi.common.core.utils.StringUtils;
import com.songqi.common.mybatis.core.util.MyBatisUtils;
import com.songqi.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author songqi(13776346982)
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService
{




    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @param language 语言
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId, String language)
    {
        return baseMapper.selectNoticeById(noticeId, language);
    }

    @Override
    public SysNoticeDetailVo selectNoticeById(Long noticeId) {
        SysNotice sysNotice = baseMapper.selectById(noticeId);
        return BeanCopyUtil.copyProperties(sysNotice, SysNoticeDetailVo.class);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @param language 语言
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNoticeQueryVo notice, String language)
    {
        return baseMapper.selectNoticeList(notice, language);
    }

    @Override
    public IPage<SysNotice> pageListNotice(SysNoticeQueryVo notice, PageDomain pageDomain, String language) {
        IPage page = MyBatisUtils.buildPage(pageDomain);
        return baseMapper.pageListNotice(page, notice, language);

    }

    /**
     * 新增公告
     *
     * @param sysNoticeAddVo 公告信息
     * @return 结果
     */
    @Override
    public void insertNotice(SysNoticeAddVo sysNoticeAddVo){
        if(! "out".equals(sysNoticeAddVo.getNoticeSource()) && StringUtils.isEmpty(sysNoticeAddVo.getNoticeContent())){
            throw new CustomException("资讯或公告内容必填");
        }
        SysNotice sysNotice = BeanCopyUtil.copyProperties(sysNoticeAddVo, SysNotice.class);
        sysNotice.setCreateBy(SecurityUtils.getUsername());
        sysNotice.setCreateTime(new Date());
        baseMapper.insert(sysNotice);


    }

    /**
     * 修改公告
     *
     * @param sysNoticeEditVo 公告信息
     * @return 结果
     */
    @Override
    public void updateNotice(SysNoticeEditVo sysNoticeEditVo)
    {
        if(! "out".equals(sysNoticeEditVo.getNoticeSource()) && StringUtils.isEmpty(sysNoticeEditVo.getNoticeContent())){
            throw new CustomException("资讯或公告内容必填");
        }
        SysNotice sysNotice = BeanCopyUtil.copyProperties(sysNoticeEditVo, SysNotice.class);
        sysNotice.setUpdateBy(SecurityUtils.getUsername());
        sysNotice.setUpdateTime(new Date());
        baseMapper.updateById(sysNotice);


    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public void deleteNoticeById(Long noticeId)
    {

        baseMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public void deleteNoticeByIds(Long[] noticeIds)
    {
        if(noticeIds != null && noticeIds.length > 0){
            for(Long id : noticeIds){
                this.deleteNoticeById(id);
            }
        }

    }

    @Override
    public List<SysNotice> selectLatestNotice(int top, String noticeType, String language) {
        return baseMapper.selectLatestNotice(top, noticeType, language);
    }

}
